/*Author:no-bald-chen
* Time:2021/8/12
* */

package DataStructure.tree;

import java.util.List;

public class BinaryTree implements Tree{
    private TreeNode root;
    private List<Integer> integers;
    private int k;
    private static final int NULL = -1;

    public BinaryTree(List<Integer> integers){
       this.integers = integers;
        k=0;
    }

    @Override
    public TreeNode getRoot() {
        return root;
    }

    @Override
    public TreeNode InitTree() {
        int num = integers.get(k++);
        if (num==NULL){
            return null;
        }
        TreeNode treeNode = new TreeNode(num,null,null);

        treeNode.l_child=InitTree();
        treeNode.r_child=InitTree();
        return treeNode;
    }

    @Override
    public void PreorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }

        System.out.print(treeNode.val+" ");
        PreorderTravel(treeNode.l_child);
        PreorderTravel(treeNode.r_child);
    }

    @Override
    public void InorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }
        InorderTravel(treeNode.l_child);
        System.out.print(treeNode.val+" ");
        InorderTravel(treeNode.r_child);
    }

    @Override
    public void PostorderTravel(TreeNode treeNode) {
        if (treeNode==null){
            return;
        }
        PostorderTravel(treeNode.l_child);
        PostorderTravel(treeNode.r_child);
        System.out.print(treeNode.val+" ");
    }
}
